﻿// ******************************************************************************
// Developed by Oguz Demirel
// Date: March 17, 2010
// ******************************************************************************
namespace BulkListActions
{
    using System;
    using System.Net;
    using System.Web;
    using System.Collections.Specialized;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Utilities;
    using System.Collections.Generic;

    public class DeleteItemsHandler : CustomHandlerBase
    {
        // This is where you override abstract class ProcessItem
        // Simply code here your custom logic which will be applied
        // on the list item
        public override void ProcessItem(SPListItem item)
        {
            // Delete item
            item.Delete();
        }

    } // end - class DeleteItemsHandler

    public class CheckOutItemsHandler : CustomHandlerBase
    {
        // This is where you override abstract class ProcessItem
        // Simply code here your custom logic which will be applied
        // on the list item
        public override void ProcessItem(SPListItem item) 
        {
            // Check Out item
            item.File.CheckOut();
        }

    } // end - class CheckOutItemsHandler

    public class CheckInItemsHandler : CustomHandlerBase
    {
        // This is where you override abstract class ProcessItem
        // Simply code here your custom logic which will be applied
        // on the list item
        public override void ProcessItem(SPListItem item)
        {
            if (//item.ParentList.ForceCheckout && 
                item.File.CheckOutStatus != SPFile.SPCheckOutStatus.None)
                // Check In item
                item.File.CheckIn("Checked-in by custom actions menu item.");
        }

    } // end - class CheckInItemsHandler

    public class PublishItemsHandler : CustomHandlerBase
    {
        // This is where you override abstract class ProcessItem
        // Simply code here your custom logic which will be applied
        // on the list item
        public override void ProcessItem(SPListItem item)
        {
            // Publish item
            item.File.Publish("Published by custom actions menu item.");
        }

    } // end - class PublishItemsHandler

    public class UnpublishItemsHandler : CustomHandlerBase
    {
        // This is where you override abstract class ProcessItem
        // Simply code here your custom logic which will be applied
        // on the list item
        public override void ProcessItem(SPListItem item)
        {
            // UnPublish item
            item.File.UnPublish("Unpublished by custom actions menu item.");
        }

    } // end - class UnpublishItemsHandler

    public class UpdateMetaDataHandler : CustomHandlerBase
    {
        // Store item IDs to process
        string itemGUIDs = "&IDs=";
        // Store list ID
        string listID = "&ListID=";
        // Store referrer url
        string referrer = "&referrer=";

        // This is where you override abstract class ProcessItem
        // Simply code here your custom logic which will be applied
        // on the list item
        public override void ProcessItem(SPListItem item)
        {
            // Store an item's ID before processing it
            itemGUIDs += item.UniqueId.ToString() + "_";

            // Only go to update metadata page after last item processed
            if (itemCount == 0)
            {
                string queryString = "meta=";

                // Construct query string
                // Assign common field internal names to query string
                // Updated metadata values need to be passed 
                // from 'commonFields' dictionary of base class
                foreach (var pair in commonFields)
                    queryString += pair.Key + "+";

                // Remove last '+' char
                queryString = queryString.Remove(queryString.LastIndexOf('+'));

                // Remove last '_' char
                itemGUIDs = itemGUIDs.Remove(itemGUIDs.LastIndexOf('_'));

                // Attach item IDs to query string
                queryString += itemGUIDs;

                // Get list id
                listID += item.ParentList.ID.ToString();
                // Attach list ID to query string
                queryString += listID;
                referrer += item.Web.Url;
                queryString += referrer;
                
                // Only go to update metadata page after last item processed
                SPUtility.Redirect(item.Web.Url + "/_layouts/Zieglers/BulkListActions/UpdateMetadata.aspx", SPRedirectFlags.Default, HttpContext.Current, queryString);

            } // end - if
        }

    } // end - class UpdateMetaDataHandler

    public class MoveItemsHandler : CustomHandlerBase
    {
        // Store item IDs to process
        string itemGUIDs = "IDs=";
        // Store list ID
        string listID = "&ListID=";
        // Store referrer url
        string referrer = "&referrer=";

        // This is where you override abstract class ProcessItem
        // Simply code here your custom logic which will be applied
        // on the list item
        public override void ProcessItem(SPListItem item)
        {
            // Move item

            // Store an item's ID before processing it
            itemGUIDs += item.UniqueId.ToString() + "_";

            // Only go to move page after last item processed
            if (itemCount == 0)
            {
                string queryString = "";

                // Remove last '_' char
                itemGUIDs = itemGUIDs.Remove(itemGUIDs.LastIndexOf('_'));

                // Attach item IDs to query string
                queryString += itemGUIDs;

                // Get list id
                listID += item.ParentList.ID.ToString();
                // Attach list ID to query string
                queryString += listID;
                referrer += item.Web.Url;
                queryString += referrer;

                // Only go to update metadata page after last item processed
                SPUtility.Redirect(item.Web.Url + "/_layouts/Zieglers/BulkListActions/Move.aspx", SPRedirectFlags.Default, HttpContext.Current, queryString);
            }

        }

    } // end - class MoveItemsHandler
    
}
